package com.ljfiction.book.activitys;


import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ljfiction.book.MyView.SuperButton;
import com.ljfiction.book.R;
import com.ljfiction.book.http.HttpManager;
import com.ljfiction.book.http.SoftHideKeyBoardUtil;
import com.ljfiction.book.tools.SettingManager;
import com.ljfiction.book.tools.StringTool;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.lzy.okhttputils.request.BaseRequest;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Headers;
import okhttp3.Response;


public class RetrieveActivity extends BaseActivity {

    @BindView(R.id.daohanglan_recheth)
    LinearLayout daohanglanRecheth;
    @BindView(R.id.back_recheath)
    ImageView backRecheath;
    @BindView(R.id.poneword_retrieve)
    EditText ponewordRetrieve;
    @BindView(R.id.yanzhengma_retrieve)
    EditText yanzhengmaRetrieve;
    @BindView(R.id.fayanzhengma_retrieve)
    SuperButton fayanzhengmaRetrieve;

    @BindView(R.id.loginbeijingret)
    ImageView loginbeijingret;
    @BindView(R.id.yizhucetishi)
    LinearLayout yizhucetishi;

    @BindView(R.id.passwordretrieve)
    EditText passwordretrieve;
    @BindView(R.id.eyeiskai)
    ImageView eyeiskai;
    @BindView(R.id.quhao_text_retrieve)
    TextView quhaoTextRetrieve;
    @BindView(R.id.nestbutton_retrieve)
    SuperButton nestbuttonRetrieve;
    private CountDownTimer countDownTimer;
    private Intent intent;
    private boolean iskai = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_retrieve);
        ButterKnife.bind(this);
        SoftHideKeyBoardUtil.assistActivity(this);
    }


    @OnClick({R.id.back_recheath, R.id.fayanzhengma_retrieve, R.id.nestbutton_retrieve, R.id.eyeiskai,R.id.quhao_retrieve})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_recheath:
                goBack();
                break;
            case R.id.fayanzhengma_retrieve:
                if (quhaoTextRetrieve.getText().toString().equals("+86")&&StringTool.isxuniphone(ponewordRetrieve.getText().toString())) {
                    showToast("请输入正确手机号");
                } else if (StringTool.isEmpty(ponewordRetrieve.getText().toString())) {
                    showToast("请输入手机号");
                } else {
                    //验证手机号
                    chechNickPhone();
                }
                break;
            case R.id.nestbutton_retrieve:
                if ("".equals(ponewordRetrieve.getText().toString())) {
                    showToast("请输入手机号");
                    return;
                } else if (StringTool.isEmpty(ponewordRetrieve.getText().toString())) {
                    showToast("请输入手机号");
                    return;
                } else if (!StringTool.isPass(passwordretrieve.getText().toString())) {
                    showToast(StringTool.isPassString(passwordretrieve.getText().toString()));
                    return;
                } else if (!"".equals(yanzhengmaRetrieve.getText().toString())) {
                    checkedCode();
                } else {
                    showToast("请输入验证码");
                }
                break;
            case R.id.eyeiskai:
                if (iskai) {
                    iskai = false;
                    passwordretrieve.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    eyeiskai.setImageResource(R.mipmap.eyebihui);
                } else {
                    iskai = true;
                    passwordretrieve.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    eyeiskai.setImageResource(R.mipmap.eyekaihui);
                }
                break;
            case R.id.quhao_retrieve:
             Intent   intent=new Intent(RetrieveActivity.this,SelectquhaoActivity.class);
                startActivityForResult(intent,10);

                break;
        }
    }

    //验证验证码
    private void checkedCode() {
        OkHttpUtils.post(HttpManager.IP + "/verification/person-checkedCode")
                .params("checkCode", yanzhengmaRetrieve.getText().toString())
                .params("phoneId", StringTool.deletadd(quhaoTextRetrieve.getText().toString())+ ponewordRetrieve.getText().toString())
                .execute(new StringCallback() {
                    @Override
                    public void onBefore(BaseRequest request) {
                        super.onBefore(request);
                        request.getHeaders().remove("cookie");
                    }

                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            if ("200".equals(jsonObject.getString("returnCode"))) {
                                intent = new Intent(RetrieveActivity.this, RetriveNextActivity.class);
                                intent.putExtra("yanzhengma", yanzhengmaRetrieve.getText().toString());
                                intent.putExtra("mima", passwordretrieve.getText().toString());
                                intent.putExtra("phone", ponewordRetrieve.getText().toString());
                                intent.putExtra("quhao",quhaoTextRetrieve.getText().toString());
                                startActivityForResult(intent, 1);
                            } else if ("500".equals(jsonObject.getString("returnCode"))) {
                                showToast(jsonObject.getString("msg"));
                            } else {
                                showToast(jsonObject.getString("msg"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });

    }

    private void chechNickPhone() {

        OkHttpUtils.get(HttpManager.IP + "/person-checkNickPhone/" + ponewordRetrieve.getText().toString())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            if ("200".equals(jsonObject.getString("returnCode"))) {
                                if (yizhucetishi.getVisibility() == View.VISIBLE) {
                                    yizhucetishi.setVisibility(View.INVISIBLE);
                                }
                                getShortMessage();
                            } else if ("500".equals(jsonObject.getString("returnCode"))) {
                                if (yizhucetishi.getVisibility() == View.INVISIBLE) {
                                    yizhucetishi.setVisibility(View.VISIBLE);
                                }
                            } else {
                                showToast("发送失败，请稍后再试");
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                });
    }

    //发验证码
    private void getShortMessage() {
        OkHttpUtils.post(HttpManager.IP + "/verification/sys-getShortMessage")
                .params("userMob",StringTool.deletadd(quhaoTextRetrieve.getText().toString())+ ponewordRetrieve.getText().toString())
                .params("type", StringTool.huoqugettype(quhaoTextRetrieve.getText().toString(),"RegisterPwd"))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            if ("200".equals(jsonObject.getString("returnCode"))) {
                                showToast("发送成功");
                                countDownTimer = new CountDownTimer(60000, 1000) {
                                    @Override
                                    public void onTick(long millisUntilFinished) {
                                        fayanzhengmaRetrieve.setText("剩" + millisUntilFinished / 1000 + "秒");
                                        fayanzhengmaRetrieve.setClickable(false);
                                        fayanzhengmaRetrieve.setsSolidColor("#D8D8D8");
                                    }

                                    @Override
                                    public void onFinish() {
                                        fayanzhengmaRetrieve.setText("重新发送");
                                        fayanzhengmaRetrieve.setClickable(true);
                                        fayanzhengmaRetrieve.setsSolidColor("#f77583");
                                    }
                                };
                                countDownTimer.start();
                            } else {
                                showToast(jsonObject.getString("msg"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == 1 && requestCode == 1) {
            setResult(10);
            goBack();
        }else if (requestCode==10&&resultCode==10){
            quhaoTextRetrieve.setText(data.getStringExtra("countryNumber"));
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
